(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var uaKey = L('urban_airship_key');
	var uaSecret = L('urban_airship_secret');
	var uaMasterSecret = L('urban_airship_master_secret');
	var uaBaseURL = L('urban_airship_base_url');
	var uaAlias = '';

	var urban_airship = {};
	urban_airship.init = function() {
		Ti.Network.registerForPushNotifications({
			types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
			success : function(e) {
				$.deviceToken(e.deviceToken);
				var params = {
					tags : ['version' + Ti.App.getVersion()],
					alias : this.alias
				};
				urban_airship.register(params, function(data) {
				}, function(errorregistration) {
				});
			},
			error : function(e) {
			},
			callback : function(e) {
				var dlg = Ti.UI.createAlertDialog({
					title : 'New Message',
					message : e.data.alert,
					extra : e.data.extra,
					buttonNames : ['OK', 'Cancel'],
					cancel : 1,
				});
				setTimeout(function() {
					dlg.show();
				}, 15000);
				dlg.addEventListener('click', function(e) {
					if (e.index === 0) {
						if (e.source && e.source.extra) {
							var extra = JSON.parse(e.source.extra);
							if (extra) {
								if (extra.productVariantID && extra.retailerStoreID && extra.userID) {
									$.offerredProductVariantID(extra.productVariantID);
									$.offerredRetailerStoreID(extra.retailerStoreID);
									$.offerredRetailerStoreID(extra.userID);
									if ($.isLoggedIn()) {
										$.notificationGoToProductDetailPage();
									} else {
										$.openLoginWindow();
									}
								} else if (extra.notificationID && extra.userID) {
									$.notificationDetailID(extra.notificationID);
									$.notificationUserID(extra.userID);
									if ($.isLoggedIn()) {
										$.notificationGoToDetailPage();
									} else {
										$.openLoginWindow();
									}
								}
							}
						}
					}
				});
			}
		});
		$.w('Urban AirShip init successfully!');
	};
	urban_airship.getToken = function() {
		return Ti.Network.remoteDeviceUUID;
	};
	urban_airship.register = function(params, lambda, lambdaerror) {
		var method = 'PUT';
		var token = urban_airship.getToken();
		var url = uaBaseURL + '/api/device_tokens/' + token;
		payload = (params) ? JSON.stringify(params) : '';
		urban_airship.helper(url, method, payload, function(data, status) {
			if (status == 200) {
				lambda({
					action : "updated",
					success : true
				});
			} else if (status == 201) {
				lambda({
					action : "created",
					success : true
				});
			} else {
				$.i('error registration: ' + JSON.stringify(status));
			}
		}, function(xhr, error) {
			$.i('xhr error registration: ' + JSON.stringify(error));
		});
	};
	urban_airship.unregister = function(lambda) {
		var method = 'DELETE';
		var token = urban_airship.getToken();
		var url = uaBaseURL + '/api/device_tokens/' + token;
		urban_airship.helper(url, method, null, function(data, status) {
			if (status == 204) {
				lambda({
					status : status
				});
			} else {
				lambda({
					status : status
				});
			}
		}, function(xhr, error) {
			lambda({
				success : false,
				xhr : xhr.status,
				error : error
			});
		});
	};
	urban_airship.getAlias = function(lambda) {
		var method = 'GET';
		var token = urban_airship.getToken();
		var url = uaBaseURL + '/api/device_tokens/' + token;
		urban_airship.helper(url, method, null, function(data, status) {
			lambda(data);
		}, function(xhr, e) {
			lambda({
				status : xhr.status,
				e : e
			});
		});
	};
	urban_airship.helper = function(url, method, params, lambda, lambdaerror) {
		var xhr = Ti.Network.createHTTPClient();
		xhr.setTimeout(60000);
		xhr.onerror = function(e) {
			lambdaerror(this, e);
		};
		xhr.onload = function() {
			var results = this.responseText;
			lambda(results, this.status);
		};
		xhr.open(method, url);
		xhr.setRequestHeader('Content-Type', 'application/json');
		xhr.setRequestHeader('Authorization', 'Basic ' + Ti.Utils.base64encode(uaKey + ":" + uaSecret));
		xhr.send(params);
	};

	module.exports = urban_airship;
})();
